package com.wanmait.zaisousuo.webcontroller;


import com.wanmait.zaisousuo.pojo.Article;
import com.wanmait.zaisousuo.pojo.ArticleComment;
import com.wanmait.zaisousuo.pojo.Userinfo;
import com.wanmait.zaisousuo.service.ArticleCommentService;
import com.wanmait.zaisousuo.service.ArticleService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/web/articleComment")
public class ArticleCommentWebController {

    @Resource
    private ArticleCommentService articleCommentService;

    @Resource
    private ArticleService articleService;

    @RequestMapping("toAddComment")
    @ResponseBody
    public void toAddComment(Integer parentCommentId, Integer articleId, String content, HttpServletRequest request){

        if(parentCommentId==null)parentCommentId=0;
        ArticleComment articleComment = new ArticleComment();
        articleComment.setContent(content);//内容

        ArticleComment articleComment1 = new ArticleComment();
        articleComment1.setId(parentCommentId);//父级id
        articleComment.setParentArticleComment(articleComment1);

        Article article = new Article();
        article.setId(articleId);//文章id
        articleComment.setArticle(article);

        Userinfo userinfo =new Userinfo();
        userinfo.setId(((Userinfo)(request.getSession().getAttribute("userinfo"))).getId());//评论用户的id
        articleComment.setUserinfo(userinfo);

        articleCommentService.insert(articleComment);

        articleService.clearCache();

    }













}
